package com.aidong.stadium.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.aidong.activity.dao.MstCampaignDao;
import com.aidong.activity.entity.Member;
import com.aidong.myprofile.dao.MyAppointmentDao;
import com.aidong.pay.dao.OrdergeneratedDao;
import com.aidong.stadium.biz.MstScheduleBiz;
import com.aidong.stadium.dao.MstScheduleDao;
import com.aidong.stadium.entity.MstConclusionabout;
import com.aidong.stadium.entity.MstCourseSchedule;
import com.aidong.stadium.entity.MstCourseType;
import com.aidong.stadium.entity.MstSchedule;
import com.aidong.stadium.entity.StoreCourse;

@Service("aboutScheduleBiz")
public class MstScheduleBizImpl implements MstScheduleBiz {

    private MstScheduleDao aboutScheduleDao;

    private MyAppointmentDao appointmentDao;

    private OrdergeneratedDao ordergeneratedDao;

    private MstCampaignDao mstCampaignDao;

    @Autowired
    public void setAppointmentDao(MyAppointmentDao appointmentDao) {
        this.appointmentDao = appointmentDao;
    }

    @Autowired
    public void setAboutScheduleDao(MstScheduleDao aboutScheduleDao) {
        this.aboutScheduleDao = aboutScheduleDao;
    }

    @Autowired
    public void setOrdergeneratedDao(OrdergeneratedDao ordergeneratedDao) {
        this.ordergeneratedDao = ordergeneratedDao;
    }

    @Autowired
    public void setMstCampaignDao(MstCampaignDao mstCampaignDao) {
        this.mstCampaignDao = mstCampaignDao;
    }

    /**
     * 查找门店下的团操课程
     *
     * @return
     */
    @Override
    public List<StoreCourse> getAboutSchedule(Map<String, Object> map) {
        return aboutScheduleDao.getAboutSchedule(map);
    }

    /**
     * 課程預約人數
     */
    public int getSchedulePerson(int courseAutoId) {
        return aboutScheduleDao.getSchedulePerson(courseAutoId);
    }

    /**
     * 团操课程介绍
     */
    @Override
    public MstConclusionabout getScheduleDuce(int autoId) {
        return aboutScheduleDao.getScheduleDuce(autoId);
    }

    /**
     * 判断该手机号码是否已经预约
     */
    @Override
    public List<Map<String, Object>> getIfAppoint(Map<String, Object> map) {
        return aboutScheduleDao.getIfAppoint(map);
    }

    /**
     * 添加团操课程预约
     */
    @Override
    public void addScheduleDuce(MstSchedule schedule) {
        aboutScheduleDao.addScheduleDuce(schedule);
        // 预约标记位
        Map<String, Object> paraMap = new HashMap<String, Object>();
        paraMap.put("status", 0);
        paraMap.put("idongId", schedule.getIdongId());
        appointmentDao.eidtMemberAppointFlg(paraMap);
    }

    /**
     * 团操可预约人数
     */
    @Override
    public Map<String, Object> getAppointIfFull(Map<String, Object> map) {
        return aboutScheduleDao.getAppointIfFull(map);
    }

    /**
     * 团操已预约人数
     */
    @Override
    public int getAppointOfPerson(Map<String, Object> map) {
        return aboutScheduleDao.getAppointOfPerson(map);
    }

    /**
     * 修改预约状态
     */
    @Override
    public void editScheduleStatus(Map<String, Object> map) {
        aboutScheduleDao.editScheduleStatus(map);
    }

    /**
     * 短信预约提醒信息
     */
    @Override
    public Map<String, Object> getCourseAppointRemind(int autoId) {
        return aboutScheduleDao.getCourseAppointRemind(autoId);
    }

    /**
     * 判断该手机号码是否在同一时间段预约了其它课程
     */
    @Override
    public List<Map<String, Object>> getOtherAppoint(Map<String, Object> map) {
        return aboutScheduleDao.getOtherAppoint(map);
    }

    /**
     * 团操课程差价
     */
    @Override
    public int getMststorePriceDiffer(int orderId) {
        return aboutScheduleDao.getMststorePriceDiffer(orderId);
    }

    /**
     * 团操课程时间安排列表
     */
    @Override
    public List<MstCourseSchedule> getScheduleTimelist(Map<String, Object> map) {
        return aboutScheduleDao.getScheduleTimelist(map);
    }

    /**
     * 团操课程列表
     */
    @Override
    public List<MstCourseType> getCourseType(Map<String, Object> map) {
        return aboutScheduleDao.getCourseType(map);
    }

    /**
     * 可提前预约天数
     */
    @Override
    public Map<String, Object> getCallableBondDay(int autoId) {
        return aboutScheduleDao.getCallableBondDay(autoId);
    }

    /**
     * 课程详情已报名人数
     */
    @Override
    public List<Map<String, Object>> getCourseAboutDetail(Map<String, Object> map) {
        return aboutScheduleDao.getCourseAboutDetail(map);
    }

    /**
     * 课程报名列表
     */
    @Override
    public List<Member> getAlreadySignedCourse(Map<String, Object> map) {
        return aboutScheduleDao.getAlreadySignedCourse(map);
    }

    /**
     * 支付信息
     */
    @Override
    public String getPayInfo(int autoId) {
        return aboutScheduleDao.getPayInfo(autoId);
    }

    /**
     * 已登录或已注册
     * 更新version字段
     */
    @Override
    public Map<String, Object> editScheduleVersion(Map<String, Object> map, MstSchedule schedule) {
        int result = aboutScheduleDao.editScheduleVersion(map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("versionResult", result);//记录更新version字段是否成功
        int subscribeId = 0;//预约ID
        String orderId = "";//订单ID
        if (result > 0) {
            aboutScheduleDao.addScheduleDuce(schedule);
            subscribeId = schedule.getSubscribeId(); //version更新成功获取 预约ID
            // 添加付款信息
            Map<String, Object> payMap = new HashMap<String, Object>();
            payMap.put("subscribeId", subscribeId);
            payMap.put("orderType", 2);
            payMap.put("storeId", schedule.getStoreId());
            ordergeneratedDao.addCourseOrderPay(payMap);
            orderId = payMap.get("orderId").toString(); // 订单ID
        }
        resultMap.put("subscribeId", subscribeId);//预约ID
        resultMap.put("orderId", orderId);//订单ID
        return resultMap;
    }

    /**
     * 未注册
     * 更新version字段
     */
    @Override
    public Map<String, Object> editScheduleVersionNoSign(Map<String, Object> map, MstSchedule schedule, Map<String, Object> regMap) {
        int result = aboutScheduleDao.editScheduleVersion(map);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("versionResult", result);//记录更新version字段是否成功
        int subscribeId = 0;//预约ID
        String orderId = "";//订单ID
        if (result > 0) {
            //注册用户信息
            mstCampaignDao.addMemberReg(regMap);
            // 注册成功后产生爱动号
            int regIdongId = Integer.parseInt(regMap.get("idongId").toString());
            schedule.setIdongId(regIdongId);

            aboutScheduleDao.addScheduleDuce(schedule);
            subscribeId = schedule.getSubscribeId(); //version更新成功获取 预约ID
            // 添加付款信息
            Map<String, Object> payMap = new HashMap<String, Object>();
            payMap.put("subscribeId", subscribeId);
            payMap.put("orderType", 2);
            payMap.put("storeId", schedule.getStoreId());
            ordergeneratedDao.addCourseOrderPay(payMap);
            orderId = payMap.get("orderId").toString(); // 订单ID
        }
        resultMap.put("subscribeId", subscribeId);//预约ID
        resultMap.put("orderId", orderId);//订单ID
        return resultMap;
    }

    @Override
    public int getScheduleAutoId(int subscribeId) {
        // TODO Auto-generated method stub
        return aboutScheduleDao.getScheduleAutoId(subscribeId);
    }

    @Override
    public void cutScheduleVersion(int autoId) {
        // TODO Auto-generated method stub
        aboutScheduleDao.cutScheduleVersion(autoId);
    }

    public int getCourseAboutCount(Map<String, Object> map) {
        return aboutScheduleDao.getCourseAboutCount(map);
    }

    /**
     * 根据课程时间ID和爱动号查看该用户是否已购买该课程
     *
     * @param map
     * @return
     */
    public Map<String, Object> getConclusByIdongId(Map<String, Object> map) {
        return aboutScheduleDao.getConclusByIdongId(map);
    }

    /*
     * 获取周几
     */
    public Map<String, Object> getScheduleWeek(Map<String, Object> map) {
        return aboutScheduleDao.getScheduleWeek(map);
    }

    /**
     * \
     * 根据课程时间ID获取课程信息
     *
     * @param map
     * @return
     */
    public MstSchedule getSubscribeDetail(Map<String, Object> map) {
        return aboutScheduleDao.getSubscribeDetail(map);
    }

    /**
     * 根据教练爱动号查询出该教练获取的票数
     *
     * @param map
     * @return
     */
    public Map<String, Object> getVoteByCaochId(Map<String, Object> map) {
        return aboutScheduleDao.getVoteByCaochId(map);
    }
}
